/**
 * Created with IntelliJ IDEA.
 * User: mkadush
 * Date: 12.09.13
 * Time: 8:34
 * To change this template use File | Settings | File Templates.
 */

var onCompleteEdit = function (treeEditor, newValue, oldValue) {
//        var treePanel = Ext.getCmp('treepanel');
//        treePanel.el.mask('Saving...', 'x-mask-loading');

    var editNode = treeEditor.editNode;
    var editNodeId = editNode.id;
    Ext.Ajax.request({
        url: '../organization/jsonUpdateNodeName',
        params: {
            id: editNodeId,
            newName: newValue
        },
        success: function (response, opts) {
//                treePanel.el.unmask();
            var responseJson = Ext.decode(response.responseText);
            if (responseJson.success !== true) {
                editNode.setText(oldValue);
                Ext.Msg.alert('Success was false');
            }
        },
        failure: function (response, opts) {
//                treePanel.el.unmask();
            editNode.setText(oldValue);
            Ext.Msg.alert('failure!');
        }
    });
};


var onEdit = function () {
    var treePanel = Ext.getCmp('treepanel');
    var selectedNode = treePanel.getSelectionModel().getSelectedNode();


    if (!this.treeEditor) {
        this.treeEditor = new Ext.tree.TreeEditor(treePanel, {}, {
            cancelOnEsc: true,
            completeOnEnter: true,
            selectOnFocus: true,
            allowBlank: false,
            listeners: {
                complete: onCompleteEdit
            }
        });
    }

    this.treeEditor.editNode = selectedNode;
    this.treeEditor.startEdit(selectedNode.ui.textNode);
};

var onCompleteAdd = function (treeEditor, newValue, oldValue) {
    var editNode = treeEditor.editNode;

    if (newValue.length <= 0) {
        editNode.remove();
        return
    }

    Ext.Ajax.request({
        url: '../organization/jsonAddNode',
        params: {
            parentId: editNode.parentNode.attributes.id,
            newName: newValue
        },
        success: function (response, opts) {

            var responseJson = Ext.decode(response.responseText);

            if (responseJson.success !== true) {
                Ext.Msg.alert('An error occured with the server.');
                editNode.remove();
            }
            else {
                editNode.setId(responseJson.id);
                editNode.setIconCls(responseJson.iconCls)
            }
        }
    });


};

var onAdd = function () {
    var treePanel = Ext.getCmp('treepanel');
    var selNode = treePanel.getSelectionModel().getSelectedNode();

    if (!this.treeEditor) {
        this.treeEditor = new Ext.tree.TreeEditor(treePanel, {}, {
            cancelOnEsc: true,
            completeOnEnter: true,
            selectOnFocus: true,
            allowBlank: false,
            listeners: {
                complete: onCompleteAdd
            }
        });
    }

    selNode.expand(null, null, function () {
        var newNodeCfg = {
            text: '',
            id: 'tmpId',
            leaf: (selNode.id.substring(0, 3) == 'pro'),
            children: []
        };
        var newNode = selNode.insertBefore(newNodeCfg, selNode.firstChild);
        this.treeEditor.editNode = newNode;
        this.treeEditor.startEdit(newNode.ui.textNode);
    }, this);


};

var onConfirmDelete = function (btn) {
    if (btn !== 'yes') {
        return
    }
    var treePanel = Ext.getCmp('treepanel');
    var selNode = treePanel.getSelectionModel().getSelectedNode();

    Ext.Ajax.request({
        url: '../organization/jsonDeleteNode',
        params: {
            id: selNode.id
        },
        success: function (response, opts) {
            var responseJson = Ext.decode(response.responseText);
            if (responseJson.success === true) {
                selNode.remove();
            } else {
                Ext.MessageBox("An error occurred with server")
            }
        }
    });
};


var onDelete = function () {
    var treePanel = Ext.getCmp('treepanel');
    var selNode = treePanel.getSelectionModel().getSelectedNode();

    if (!selNode) {
        return;
    }
    Ext.MessageBox.confirm('Вы уверены?', 'Подтвертите удаление: ' + selNode.attributes.text, onConfirmDelete);
};

